package com.framework.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.framework.entity.sys.SysAddress;
import com.framework.entity.sys.SysAgent;
import com.framework.exception.RRException;
import com.framework.exception.menu.RespCode;
import com.framework.dao.AddressDao;
import com.framework.service.AddressService;
import com.framework.service.AgentService;
import com.framework.utils.string.StringUtil;
import com.framework.validator.Assert;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author wq
 */
@Service("addressService")
public class AddressServiceImpl extends ServiceImpl<AddressDao, SysAddress> implements AddressService {

	@Autowired
	private AgentService agentService;


	@Override
	public List<SysAddress> queryAddressList(String token) {
		Assert.isNull(token, "参数错误");
		SysAgent agent = agentService.queryAgentByToken(token);
		return baseMapper.queryAddressList(agent.getAgentId());
	}

	@Transactional(rollbackFor=Exception.class)
	@Override
	public void insertAddress(SysAddress address, String token) {
		Assert.isNull(address, "参数错误");
		if(StringUtils.isBlank(address.getArea())||
				StringUtils.isBlank(address.getCity())||
				StringUtils.isBlank(address.getInfo())||
				StringUtils.isBlank(address.getNick())||
				StringUtils.isBlank(address.getProv())||
				address.getIsDefault() == null||
				StringUtils.isBlank(token)||
				address.getIsDefault() == null||
				StringUtils.isBlank(address.getPhone())){
			throw new RRException(RespCode.NOT_PARAM_ERROR);
		}
		String addressId = StringUtil.getDateString(10);
		SysAgent agent = agentService.queryAgentByToken(token);
		address.setAgentId(agent.getAgentId());
		address.setCreateDate(new Date());
		address.setAddressId(addressId);
		baseMapper.insertAddress(address);
		//设置成默认
		if(address.getIsDefault() == 1){
			setDefault(addressId);
		}
	}

	@Transactional(rollbackFor=Exception.class)
	@Override
	public void updateAddress(SysAddress address, String token) {
		Assert.isNull(address, "参数错误");
		if(StringUtils.isBlank(address.getArea())||
				StringUtils.isBlank(address.getAddressId())||
				StringUtils.isBlank(address.getCity())||
				StringUtils.isBlank(address.getInfo())||
				StringUtils.isBlank(address.getNick())||
				StringUtils.isBlank(address.getProv())||
				address.getIsDefault() == null||
				StringUtils.isBlank(token)||
				StringUtils.isBlank(address.getPhone())){
			throw new RRException(RespCode.NOT_PARAM_ERROR);
		}
		String addressId = address.getAddressId();
		//修改之前校验
		infoAddress(token,addressId);
		baseMapper.updateAddressById(address);
		//设置成默认
		if(address.getIsDefault() == 1){
			setDefault(addressId);
		}
		queryDefaultAddress(token);
	}

	@Override
	public SysAddress infoAddress(String token, String addressId) {
		if(StringUtils.isBlank(token) ||
				StringUtils.isBlank(addressId)){
			throw new RRException(RespCode.NOT_PARAM_ERROR);
		}
		SysAgent agent = agentService.queryAgentByToken(token);
		Long agentId = agent.getAgentId();
		SysAddress address = new SysAddress();
		address.setAddressId(addressId);
		SysAddress sysAddress = baseMapper.selectOne(address);
		if(sysAddress == null){
			throw new RRException("9000","未查询到收货地址信息");
		}
		if(agentId.intValue() != sysAddress.getAgentId()){
			throw new RRException("9000","此收货地址不属于当前登录人");
		}
		return sysAddress;
	}

	@Transactional(rollbackFor=Exception.class)
	@Override
	public void setDefault(String addressId) {
		SysAddress address = new SysAddress();
		address.setAddressId(addressId);
		SysAddress sysAddress = baseMapper.selectOne(address);
		if(sysAddress != null){
			Long agentId = sysAddress.getAgentId();
			baseMapper.updateDefaultByAgentId(agentId);
			baseMapper.isDefaultById(addressId);
		}
	}

	@Override
	public SysAddress queryDefaultInfo(Long agentId) {
		if(agentId == null){
			throw new RRException(RespCode.NOT_PARAM_ERROR);
		}
		return baseMapper.queryDefaultInfo(agentId);
	}

	@Override
	public SysAddress queryDefaultAddress(String token) {
		SysAgent agent = agentService.queryAgentByToken(token);
		SysAddress address = queryDefaultInfo(agent.getAgentId());
		if(address==null){
			address=checkDefaultAddress(agent.getAgentId());
		}

		if(address==null){
			throw new RRException(RespCode.NOT_DEFAULT_ADDRESS);
		}
		return address;
	}

	@Override
	public SysAddress queryAddressById(String addressId) {
		SysAddress address = new SysAddress();
		address.setAddressId(addressId);
		return baseMapper.selectOne(address);
	}

	@Override
	public void delAddress(String addressId) {
		if(StringUtils.isBlank(addressId)){
			throw new RRException(RespCode.NOT_PARAM_ERROR);
		}
		baseMapper.delAddress(addressId);
	}

	@Override
	public SysAddress checkDefaultAddress(Long agentId) {

		List<SysAddress> addressList = selectList(new EntityWrapper<SysAddress>()
				.eq("agent_id", agentId)
				.eq("is_show", 1)
				.orderBy("addressId",true)
		);
		if(addressList.size()>0){
			setDefault(addressList.get(0).getAddressId());
			return addressList.get(0) ;
		}else {
			return null;
		}

	}

}
